package cn.wolfcode.web.controller;


import cn.wolfcode.transfer.domain.SystemDictionary;
import cn.wolfcode.transfer.domain.SystemDictionaryItem;
import cn.wolfcode.transfer.qo.JsonResult;
import cn.wolfcode.transfer.qo.SystemDictionaryQueryObject;
import cn.wolfcode.transfer.service.ISystemDictionaryItemService;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.ISystemDictionaryService;
import cn.wolfcode.transfer.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiredPermission(name = "字典明细页面", expression = "systemDictionaryItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryQueryObject qo) {
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("SystemDictionaries",systemDictionaries);
        model.addAttribute("qo",qo);
        return "systemDictionaryItem/list";
    }

    @RequestMapping("/SystemDictionarySelect")
    @ResponseBody
    public PageInfo<SystemDictionaryItem> SystemDictionarySelect(Long id) {
        SystemDictionaryQueryObject qo = new SystemDictionaryQueryObject();
        qo.setSystemDictionaryId(id);
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        return pageInfo;
    }
    @RequestMapping("/selectByTypeSN")
    @ResponseBody
    public List<SystemDictionaryItem> selectByTypeSN(String sn) {
        List<SystemDictionaryItem> list = systemDictionaryItemService.selectByTypeSN(sn);
        return list;
    }
    @RequestMapping("/selectByParent")
    @ResponseBody
    public List<SystemDictionaryItem> selectByParent(Long id) {
        List<SystemDictionaryItem> list = systemDictionaryItemService.selectByParent(id);
        return list;
    }
    @RequestMapping("/selectParent")
    public String selectParent(Model model,QueryObject qo , Long ParentId) {
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.selectParent(qo, ParentId);
        model.addAttribute("pageInfo", pageInfo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("SystemDictionaries",systemDictionaries);
        model.addAttribute("qo",qo);
        return "systemDictionaryItem/list";
    }



    @RequiredPermission(name = "字典明细删除", expression = "systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @RequiredPermission(name = "字典明细新增/编辑", expression = "systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() == null) {
            systemDictionaryItemService.save(systemDictionaryItem);
        } else {
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }



}
